/**
 * @file base.js
 *
 * Some basic behaviors and utility functions for Views.
 */

Drupal.Views = {};

/**
 * jQuery UI tabs, Views integration component
 */
Drupal.behaviors.viewsTabs = function(context) {
    $('#views-tabset:not(.views-processed)').addClass('views-processed').each(function() {
        new Drupal.Views.Tabs($(this), {selectedClass: 'active'});
    });

    $('a.views-remove-link')
            .addClass('views-processed')
            .click(function() {
        var id = $(this).attr('id').replace('views-remove-link-', '');
        $('#views-row-' + id).hide();
        $('#views-removed-' + id).attr('checked', true);
        return false;
    });
}

/**
 * For IE, attach some javascript so that our hovers do what they're supposed
 * to do.
 */
Drupal.behaviors.viewsHoverlinks = function() {
    if ($.browser.msie) {
        // If IE, attach a hover event so we can see our admin links.
        $("div.view:not(.views-hover-processed)").addClass('views-hover-processed').hover(
                function() {
                    $('div.views-hide', this).addClass("views-hide-hover");
                    return true;
                },
                function() {
                    $('div.views-hide', this).removeClass("views-hide-hover");
                    return true;
                }
        );
        $("div.views-admin-links:not(.views-hover-processed)")
                .addClass('views-hover-processed')
                .hover(
                function() {
                    $(this).addClass("views-admin-links-hover");
                    return true;
                },
                function() {
                    $(this).removeClass("views-admin-links-hover");
                    return true;
                }
        );
    }
}

/**
 * Helper function to parse a querystring.
 */
Drupal.Views.parseQueryString = function(query) {
    var args = {};
    var pos = query.indexOf('?');
    if (pos != -1) {
        query = query.substring(pos + 1);
    }
    var pairs = query.split('&');
    for (var i in pairs) {
        if (typeof(pairs[i]) == 'string') {
            var pair = pairs[i].split('=');
            // Ignore the 'q' path argument, if present.
            if (pair[0] != 'q' && pair[1]) {
                args[pair[0]] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
            }
        }
    }
    return args;
};

/**
 * Helper function to return a view's arguments based on a path.
 */
Drupal.Views.parseViewArgs = function(href, viewPath) {
    var returnObj = {};
    var path = Drupal.Views.getPath(href);
    // Ensure we have a correct path.
    if (viewPath && path.substring(0, viewPath.length + 1) == viewPath + '/') {
        var args = decodeURIComponent(path.substring(viewPath.length + 1, path.length));
        returnObj.view_args = args;
        returnObj.view_path = path;
    }
    return returnObj;
};

/**
 * Strip off the protocol plus domain from an href.
 */
Drupal.Views.pathPortion = function(href) {
    // Remove e.g. http://example.com if present.
    var protocol = window.location.protocol;
    if (href.substring(0, protocol.length) == protocol) {
        // 2 is the length of the '//' that normally follows the protocol
        href = href.substring(href.indexOf('/', protocol.length + 2));
    }
    return href;
};

/**
 * Return the Drupal path portion of an href.
 */
Drupal.Views.getPath = function(href) {
    href = Drupal.Views.pathPortion(href);
    href = href.substring(Drupal.settings.basePath.length, href.length);
    // 3 is the length of the '?q=' added to the url without clean urls.
    if (href.substring(0, 3) == '?q=') {
        href = href.substring(3, href.length);
    }
    var chars = ['#', '?', '&'];
    for (i in chars) {
        if (href.indexOf(chars[i]) > -1) {
            href = href.substr(0, href.indexOf(chars[i]));
        }
    }
    return href;
};
